package com.ctgu.牛客网.动态规划;

import java.io.*;
import java.util.Scanner;

/**
 * @author: Backdiy
 * @data: 2021/4/2 20:39
 * @desc:
 */
public class 小朋友的数字 {
    static long[] nums;
    static long[] sums;
    static long[] anss;
    static int n;
    static int p;
    static long max;
    static long maxx;

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        p = input.nextInt();
        nums = new long[n + 10];
        sums = new long[n + 10];
        anss = new long[n + 10];
        for (int i = 0; i < n; i++) {
            nums[i] = input.nextLong();
        }
        sums[0] = nums[0];
        max = nums[0];
        maxx = nums[0];
        for (int i = 1; i < n; i++) {
            max = Math.max(nums[i], max + nums[i]);
            sums[i] = Math.max(max, maxx);
            maxx = Math.max(maxx, sums[i]);
        }
        anss[0] = nums[0];
        anss[1] = 2 * nums[0];
        maxx = 0;
        for (int i = 2; i < n; i++) {
            if (sums[i - 1] > 0) {
                anss[i] = sums[i - 1] + anss[i - 1];
                if (anss[i] > anss[0]) {
                    maxx = 1;
                    anss[i] %= p;
                }
            } else {
                anss[i] = anss[1];
            }
        }
        if (maxx == 1) {
            System.out.println(anss[n - 1] % p);
        } else {
            System.out.println(anss[0] % p);
        }
//        Input.out.flush();
    }
}

class Input {
    static StreamTokenizer streamTokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    static String readLine() throws IOException {
        return bufferedReader.readLine();
    }

    static double nextDouble() throws IOException {
        streamTokenizer.nextToken();
        return (double) streamTokenizer.nval;
    }

    static int nextInt() throws IOException {
        streamTokenizer.nextToken();
        return (int) streamTokenizer.nval;
    }

    static long nextLongInt() throws IOException {
        streamTokenizer.nextToken();
        return (long) streamTokenizer.nval;
    }
}